from pprint import pprint

from django.shortcuts import render
from django.views import View
from django.http import JsonResponse

from apps.areas.models import Area
from django.forms import model_to_dict

# Create your views here.

class AreasList(View):

    def get(self, request):
        province = Area.objects.values('id', 'name').filter(parent_id=None)
        return JsonResponse({'code': 0, 'errmsg': 'OK', 'province_list': list(province)})


class AreasSubList(View):

    def get(self, request, pk):
        province = Area.objects.filter(id=pk).first()
        sub = province.subs.values('name', 'id').all()
        province = model_to_dict(province)
        province['subs'] = list(sub)
        return JsonResponse({'code': 0, 'errmsg': 'OK', 'sub_data': province})
